﻿using System.Collections.Generic;
using System.Data;
using RpcHelper;
using RpcModel;
using SqlExecHelper;
using SqlExecHelper.SetColumn;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Service.DAL
{
        internal class ServerGroupDAL : SqlExecHelper.SqlBasicClass
        {
                public ServerGroupDAL() : base("ServerGroup")
                {

                }
                public bool CheckIsRepeat(string typeVal)
                {
                        return base.CheckIsExists( new SqlWhere("TypeVal", SqlDbType.VarChar, 50) { Value = typeVal });
                }
                public long Add(ServerGroupDatum datum)
                {
                        base.Insert(datum, out long id);
                        return id;
                }

                public void Set(long id, string name)
                {
                        if(!base.Update(new ISqlSetColumn[] {
                                new SqlSetColumn("GroupName", SqlDbType.NVarChar,50){Value=name}
                        }, new SqlWhere("Id", SqlDbType.BigInt) { Value = id }))
                        {
                                throw new ErrorException ("rpc.server.group.set.error");
                        }
                }
                public ServerGroup[] Query (string name, IBasicPage paging, out long count)
                {
                        List<ISqlWhere> wheres = new List<ISqlWhere>();
                        if (!name.IsNull())
                        {
                                wheres.Add(new LikeSqlWhere("GroupName", 50) { Value = name });
                        }
                        return base.Query<ServerGroup> ("Id desc", paging.Index, paging.Size,  out count, wheres.ToArray());
                }
                public ServerGroup[] Gets ()
                {
                        return base.Get<ServerGroup> ();
                }
                public ServerGroup Get (long id)
                {
                        return base.GetRow<long, ServerGroup> ("Id", id);
                }

                public void Drop(long id)
                {
                        if(!this.Drop("Id", id))
                        {
                                throw new ErrorException ("rpc.server.group.drop.error");
                        }
                }

                public ServerGroup[] GetServiceGroup (long[] ids)
                {
                        return this.Get<long,ServerGroup> ("Id", ids);
                }
        }
}
